/*
Author: yang_wanjun
Create: 2011/05/31
Update: 2011/05/31
 */

/**
 * 移动下拉框列表中选择项的位置，对其内容进行排序
 */
Operation.prototype.move_select = function(selectid, down, hiddenid) {
	var select = document.getElementById(selectid);
	if (select.selectedIndex != -1) {
		var pass = true; // 是否可以继续执行
		if (down) {
			// 向下移动时，判断是否到达最后一项
			if (select.selectedIndex != select.options.length - 1) {
				var i = select.selectedIndex + 1;
			} else {
				pass = false;
			}
		} else {
			// 向上移动时，判断是否到达第一项
			if (select.selectedIndex != 0) {
				var i = select.selectedIndex - 1;
			} else {
				pass = false;
			}
		}

		if (pass) {
			var swapOption = new Object();
			swapOption.text = select.options[select.selectedIndex].text;
			swapOption.value = select.options[select.selectedIndex].value;
			swapOption.selected = select.options[select.selectedIndex].selected;
			for ( var property in swapOption)
				select.options[select.selectedIndex][property] = select.options[i][property];
			for ( var property in swapOption)
				select.options[i][property] = swapOption[property];
		}
	}
	var hidden = document.getElementById(hiddenid);
	hidden.value = this.get_ordered_values(selectid);
}

/**
 * 获取排序后的下拉框列表中的值，返回JSON格式
 */
Operation.prototype.get_ordered_values = function(selectid) {
	var select = document.getElementById(selectid);
	var values = '';
	for (i = 1; i <= select.options.length; i++) {
		values += '{"order":' + i + ',"value":' + select.options[i - 1].value
				+ '},';
	}
	if (values.length > 1) {
		values = values.substring(0, values.length - 1);
	}
	return '{"orders":[' + values + ']}';
}

/**
 * 把页面元素移动到指定元素的下面，例如智能感应下拉列表
 * 
 * targetid: 要移动的元素的id
 * 
 * movetoid: 要移动到的目标元素的ID，即要移动到的地方
 */
Operation.prototype.move_to = function(targetid, movetoid) {
	var targetObj = document.getElementById(targetid);
	var movetoObj = document.getElementById(movetoid);
	
	if(targetObj==null||movetoObj==null){
		return
	}

	var width = 100;
	// 取得下拉框相对于页面的左偏移量
	var left = senbz.utils.calculate_offset(movetoObj, "offsetLeft");
	// 取得下拉框相对于页面的向上偏移量，为要依附的控件的top加上其高度值
	var top = senbz.utils.calculate_offset(movetoObj, "offsetTop")
			+ movetoObj.offsetHeight;

	targetObj.style.border = "1px solid black";
	targetObj.style.left = left + "px";
	targetObj.style.top = 20 + "px";
	targetObj.style.width = width + "px";

	targetObj.style.position = "absolute";
	targetObj.style.display = "block";
	targetObj.style.zIndex = 1;
}

/**
 * 隐藏页面元素
 */
Operation.prototype.hide = function(targetid){
	var targetObj  = document.getElementById(targetid);
	if(targetObj!=null){
		targetObj.style.display = "none";
	}
}















